<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link rel="icon" href="./img/husky_tran.png" type="image/x-icon"/>
    <link rel="stylesheet" type="text/css" href="./css/login.css">
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<style>
    body {
        font-family: "Helvetica Neue", Helvetica, Arial, "PingFang SC", "Hiragino Sans GB", "Heiti SC", "MicrosoftYaHei", "WenQuanYi Micro Hei", sans-serif;
    }
</style>
<div class="login" style="margin-top: 30vh">
    <div class="login-triangle"></div>

    <h2 class="login-header">登录</h2>

    <form class="login-container" method="post">
        <p><input type="text" placeholder="用户名" id="username" value=""></p>
        <p><input type="password" placeholder="密码" id="password"></p>
        <p><input type="button" value="登录" onclick="login()"></p>
        <p>
        <div style="text-align: center;color: #4187d7;font-size: 2em" onclick="toreg()">未注册？点我去注册</div>
        </p>
    </form>
</div>

<script type="text/javascript">

    // window.localStorage
    // let locals = localStorage.getItem('reg_username');
    // console.log(locals)
    // userName = JSON.parse(locals);
    // console.log(userName)
    // userName = userName.username
    // userName = locals.username;
    // console.log('regname:' + userName)

    // document.getElementById("username").value=userName;

    function toreg() {
        window.location.href = "register.html";
    }

    var logindata = new FormData()

    function login() {
        var username = document.getElementById('username').value;
        var password = document.getElementById('password').value;
        logindata.append('username', username);
        logindata.append('password', password);
        var host = 'http://124.70.36.92:19132/';
        var url = host + 'authorities/login/'
        axios({
            method: 'POST',
            url: url,
            // url: 'http://124.70.36.92:19132/polls/login/',
            dataType: 'json',
            data: logindata,
        }).then(res => {
            if (res.data.msg.length === 0) {
                console.log(res.data)
                // console.log(res.data.data.username)
                // window.localStorage
                localStorage.setItem('userdata', JSON.stringify(res.data.data));

                // let locals = localStorage.getItem('userdata');
                // console.log('locals: ' + locals);

                window.location.href = "recommend.html";
            } else {
                console.log(res.data.msg)
                alert(res.data.msg)
            }
        }).catch(err => {
            console.log(err)
        })

    }
</script>

</body>

</html>